package com.cweb.admin.model.sys.sys_user_info.service;

import com.cweb.admin.model.sys.sys_user_info.entity.SysUserInfo;
import com.cweb.admin.serivce.authority.UserLoginService;
import com.cweb.admin.serivce.authority.UserLoginToken;
import com.cweb.framework.pojo.CustomException;
import com.cweb.framework.util.MybatisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service
public class SysUserInfoProcessService {
    @Autowired
    private SysUserInfoService sysUserInfoService;
    @Autowired
    private UserLoginService userLoginService;

    /**
     * 显示用户信息
     */
    public SysUserInfo showUserInfo(Long id) {
        return sysUserInfoService.getBaseMapper().showUserInfo(id);
    }

    /**
     * 相关的统计信息
     */
    public Map userCountInfo(Long id) {
        return sysUserInfoService.getBaseMapper().userCountInfo(id);
    }

    /**
     * 取得当前的用户信息
     */
    public UserLoginToken get() throws CustomException {
        if (null == userLoginService.getCurLoginUserId()) {
            return null;
        }
        return new UserLoginToken(sysUserInfoService.getBaseMapper().showUserInfo(userLoginService.getCurLoginUserId()), null);
    }

    /**
     * 修改密码
     */
    public Long processChangePassowrd(String password, String oldPassword) throws CustomException {
        // 取得当前用户登录信息
        UserLoginToken userLoginToken = userLoginService.getCurLoginToken();
        // 一般用户
        return sysUserInfoService.changePassowrd(userLoginToken.getId(), password, oldPassword);
    }

    /**
     * 修改用户信息
     */
    public Object processChangeUserInfo(SysUserInfo param) throws CustomException {
        // 取得当前用户登录信息
        UserLoginToken userLoginToken = userLoginService.getCurLoginToken();
        // 查询
        SysUserInfo sysUserInfo = MybatisUtil.getByIdForUpdate(sysUserInfoService, userLoginToken.getId());
        if (null != param.getHeadPhoto()) sysUserInfo.setHeadPhoto(param.getHeadPhoto());
        if (null != param.getNickName()) sysUserInfo.setNickName(param.getNickName());
        // 更新
        return sysUserInfoService.updateById(sysUserInfo);
    }
}
